package wangyachuan.sptingboot.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import wangyachuan.sptingboot.model.user;

import java.util.List;

@SuppressWarnings("all")
@Mapper
public interface UserMapper {


    // 定义插入的方法
    @Insert("insert into user(username,password,name,phone,email,address,avatar ) " +
            "values(#{username},#{password},#{name},#{phone},#{email},#{address},#{avatar})")
    void insert(user user);


    // 定义更新的方法
    // 使用 @Update 注解来定义更新方法
    @Update("UPDATE user SET username=#{username}, password=#{password}, name=#{name}, phone=#{phone}, email=#{email}, " +
            "address=#{address}, avatar=#{avatar} WHERE id=#{id}")
    void updateUser(user user);


    // 定义删除的方法
    @Update("DELETE FROM user WHERE id=#{id}")
    void deleteUser(Integer id);

    // 定义批量删除的方法
    @Update("DELETE FROM user WHERE id in ")
    void deleteUserBatch(List<Integer> ids);


    // 定义查询单个用户的方法(id为参数)
    @Select("SELECT * FROM user WHERE id = #{id}")
    user selectID(Integer id);

    // 定义查询所有用户的方法
    @Select("SELECT * FROM user ")
    // 查询所有用户，按照 id 倒序排列显示
    List<user> selectAll();

    // 定义查询name包含指定字符串的用户的方法
    @Select("SELECT * FROM user WHERE username = #{username}")
    List<user> getUserByName(String name);

    // 定义查询用户名或姓名包含指定字符串的用户的方法
    @Select("SELECT * FROM user WHERE username = #{username} and name = #{name}")
    List<user> getUserByMore(String username, String name);

    // 定义多条件模糊查询用户的方法
    @Select("SELECT * FROM user WHERE username LIKE CONCAT('%', #{username}, '%') AND name LIKE CONCAT('%', #{name}, '%') ORDER BY id DESC")
    List<user> getUserByMoreAndName(String username, String name);

    // 定义分页模糊查询用户的方法
    @Select("SELECT * FROM user WHERE username LIKE CONCAT('%', #{username}, '%') AND name LIKE CONCAT('%', #{name}, '%') ORDER BY id DESC LIMIT #{PageNum}, #{pageSize}")
    List<user> getUserByPage(Integer PageNum, Integer pageSize, String username, String name);

    @Select("SELECT count(id) FROM user WHERE username LIKE CONCAT('%', #{username}, '%') AND name LIKE CONCAT('%', #{name}, '%') ORDER BY id DESC ")
    int getUsercountByPage(String username, String name);

    //
    @Select("SELECT * FROM user WHERE username = #{username}")
    user selectByName(String username);
}
